﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BusinessLogic;

namespace AccountingGui.Views
{
    public partial class PaymentCardForm : Form
    {
        public PaymentCardForm()
        {
            InitializeComponent();
        }

        private void bt_CreatePayment_Click(object sender, EventArgs e)
        {
            int orderId;
            if (!int.TryParse(orderIdTextBox.Text, out orderId))
            {
                MessageBox.Show("Ordre id skal være numerisk", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            decimal amount;
            if (!decimal.TryParse(amountTextBox.Text, out amount))
            {
                MessageBox.Show("Amount skal være numerisk", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            using (var context = new WGRUSEntities())
            {
                var order = context.Orders.FirstOrDefault(o => o.OrdreId == orderId);
                if(order == null)
                {
                    MessageBox.Show("Ordre findes ikke", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                Payment payment = new Payment
                {
                    Amount = amount,
                    PaymentDate = DateTime.Now
                };

                order.Payments.Add(payment);
                order.OrderStateId = (int)OrderStateEnum.PaymentRecieved;

            }
        }
    }
}
